#!/bin/sh
usage() {
    echo "$0 <prefix>"
}

migemo_name=cmigemo-1.3c
prefix=$1

if [ -n "$prefix" ]; then
    if [ ! -d "$migemo_name" ]; then
        if [ ! -f "$migemo_name.tar.bz2" ]; then
            wget http://www.kaoriya.net/dist/var/$migemo_name.tar.bz2
        fi
        tar xjf $migemo_name.tar.bz2
    fi
    cd $migemo_name && \
        cd compile && \
        patch -f < ../../unix.mak.diff || : && \
        cd ../src && \
        patch -f < ../../romaji.c.diff || : && \
        cd .. && \
        ./configure && \
        cp config.mk config.mk.bak && \
        sed -e "s|^prefix[[:space:]]*=.*|prefix = $prefix|" config.mk.bak > config.mk && \
        make gcc && \
        make gcc-dict && \
        cd dict && \
        make utf-8 && \
        cd .. && \
        make gcc-install
else
    usage
fi
